//
//  ByCellShopCarListItem.m
//  oc_knowledge
//
//  Created by pro on 17/1/13.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellShopCarListItem.h"

@implementation ByCellShopCarListItem
{
    UIButton    *_selectBtn;            // 选中视图
    UIImageView *_mainView;             // 主图
    UIView      *_sepLine;              // 两个cell之间的分割
    
    UILabel     *_titleLabel;           // 标题
    UILabel     *_currentPriceLabel;    // 当前价格
    UILabel     *_originPriceLabel;     // 原价

    UILabel     *_countTitleLabel;      // 数量标题
    UILabel     *_countModifyLabel;     // 数量视图（可修改）
    UIButton    *_countAddBtn;          // 数量视图（+）
    UIButton    *_countDeleteBtn;       // 数量视图（-）
    
    UILabel     *_transferLabel;        // 配送方式
    UILabel     *_additionLabel;        // 附加label（自取：显示地址，快递：显示当日达）
    
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupUI];
        [self layout];
    }
    return self;
}

- (void)setupUI {
    
    // 选中视图
    _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_selectBtn setImage:[UIImage imageNamed:@"购物车未选中"] forState:UIControlStateNormal];
    [_selectBtn setImage:[UIImage imageNamed:@"购物车选中"] forState:UIControlStateSelected];
    [_selectBtn addTarget:self action:@selector(select) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_selectBtn];
    
    // 主图
    _mainView = [[UIImageView alloc] init];
    _mainView.backgroundColor = BY_BLUE_COLOR;
    _mainView.layer.cornerRadius = 4;
    _mainView.layer.masksToBounds = true;
    [self.contentView addSubview:_mainView];
    
    // 分割
    _sepLine = [[UIView alloc] init];
    _sepLine.backgroundColor = BY_LINE_COLOR;
    [self.contentView addSubview:_sepLine];
    
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = FONT(14);
    _titleLabel.text = @"发了纠纷说多了纠纷说多了纠纷说多了纠纷说多了纠纷说多了纠纷说多了纠纷说多分";
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_titleLabel];
    
    // 现价
    _currentPriceLabel = [[UILabel alloc] init];
    _currentPriceLabel.adjustsFontSizeToFitWidth = true;
    _currentPriceLabel.font = FONT(18);
    _currentPriceLabel.textColor = BY_RED_COLOR;
    _currentPriceLabel.text = @"￥88888.88";
    _currentPriceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_currentPriceLabel];
    
    // 原价
    _originPriceLabel = [[UILabel alloc] init];
    _originPriceLabel.adjustsFontSizeToFitWidth = true;
    _originPriceLabel.font = FONT(14);
    _originPriceLabel.textColor = [UIColor lightGrayColor];
    _originPriceLabel.textAlignment = NSTextAlignmentLeft;
    _originPriceLabel.text = @"￥99999.99";
    [self.contentView addSubview:_originPriceLabel];
    
    // 数量标题
    _countTitleLabel = [[UILabel alloc] init];
    _countTitleLabel.font = FONT(14);
    _countTitleLabel.textColor = [UIColor lightGrayColor];
    _countTitleLabel.textAlignment = NSTextAlignmentLeft;
    _countTitleLabel.text = @"购买数量：";
    [self.contentView addSubview:_countTitleLabel];
    
    // 创建数量修改视图
    _countModifyLabel = [[UILabel alloc] init];
    _countModifyLabel.backgroundColor = [UIColor whiteColor];
    _countModifyLabel.userInteractionEnabled = true;
    _countModifyLabel.font = FONT(16);
    _countModifyLabel.text = @"1";
    _countModifyLabel.textColor = [UIColor blackColor];
    _countModifyLabel.textAlignment = NSTextAlignmentCenter;
    _countModifyLabel.layer.borderColor = BY_LINE_COLOR.CGColor;
    _countModifyLabel.layer.borderWidth = 1;
    _countModifyLabel.layer.masksToBounds = true;
    _countModifyLabel.layer.cornerRadius = 13;
    [self.contentView addSubview:_countModifyLabel];
    
    // 线条
    UIView *line1 = [UIView new];
    line1.backgroundColor = BY_LINE_COLOR;
    [_countModifyLabel addSubview:line1];
    UIView *line2 = [UIView new];
    line2.backgroundColor = BY_LINE_COLOR;
    [_countModifyLabel addSubview:line2];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@1);
        make.height.equalTo(_countModifyLabel).multipliedBy(0.65);
        make.centerY.equalTo(@0);
        make.centerX.equalTo(_countModifyLabel.mas_centerX).multipliedBy(1.0 / 3 * 2);
    }];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.equalTo(line1);
        make.centerX.equalTo(_countModifyLabel.mas_centerX).multipliedBy(1.0 / 3 * 4);
    }];
    
    _countDeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _countDeleteBtn.titleLabel.font = FONT(25);
    [_countDeleteBtn setTitle:@"-" forState:UIControlStateNormal];
    [_countDeleteBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_countDeleteBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [_countDeleteBtn addTarget:self action:@selector(countDelete) forControlEvents:UIControlEventTouchUpInside];
    [_countModifyLabel addSubview:_countDeleteBtn];
    
    _countAddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _countAddBtn.titleLabel.font = FONT(25);
    [_countAddBtn setTitle:@"+" forState:UIControlStateNormal];
    [_countAddBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_countAddBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [_countAddBtn addTarget:self action:@selector(countAdd) forControlEvents:UIControlEventTouchUpInside];
    [_countModifyLabel addSubview:_countAddBtn];
    
    // 配送方式
    _transferLabel = [[UILabel alloc] init];
    _transferLabel.font = FONT(14);
    _transferLabel.textColor = [UIColor lightGrayColor];
    _transferLabel.textAlignment = NSTextAlignmentLeft;
    _transferLabel.text = @"配送方式：自取";
    [self.contentView addSubview:_transferLabel];
    
    // 附加项
    _additionLabel = [[UILabel alloc] init];
    _additionLabel.backgroundColor = BY_THEME_COLOR;
    _additionLabel.layer.cornerRadius = 4;
    _additionLabel.layer.masksToBounds = true;
    _additionLabel.font = FONT(16);
    _additionLabel.textColor = BY_GOLD_COLOR;
    _additionLabel.textAlignment = NSTextAlignmentCenter;
    _additionLabel.text = @"当日达";
    [self.contentView addSubview:_additionLabel];
}

- (void)layout {
    
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(@0);
        make.width.equalTo(@30);
    }];
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_selectBtn.mas_right);
        make.top.equalTo(@10);
        make.bottom.equalTo(@-10);
        make.width.equalTo(_mainView.mas_height);
    }];
    [_sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.height.equalTo(@1);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_mainView.mas_right).offset(10);
        make.top.equalTo(_mainView);
        make.right.equalTo(@-5);
    }];
    [_currentPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel);
        make.top.equalTo(_titleLabel.mas_bottom).offset(5);
        make.right.equalTo(_originPriceLabel.mas_left).offset(-5);
    }];
    [_originPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_currentPriceLabel);
        make.right.lessThanOrEqualTo(_titleLabel);
    }];
    [_countTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel);
        make.height.equalTo(@26);
        make.top.equalTo(_currentPriceLabel.mas_bottom).offset(5);
        make.width.equalTo(@75);
    }];
    [_countModifyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_countTitleLabel.mas_right).offset(0);
        make.right.equalTo(@-5);
        make.height.centerY.equalTo(_countTitleLabel);
    }];
    [_countDeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(@0);
        make.width.equalTo(_countModifyLabel).multipliedBy(1.0 / 3);
    }];
    [_countAddBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(@0);
        make.width.equalTo(_countDeleteBtn);
    }];
    [_transferLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_countTitleLabel);
        make.top.equalTo(_countTitleLabel.mas_bottom).offset(5);
    }];
    [_additionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_transferLabel);
        make.top.equalTo(_transferLabel.mas_bottom).offset(5);
        make.size.mas_offset(CGSizeMake(100, 32));
    }];
    
}

#pragma mark - 点击事件
- (void)select {
    
    _selectBtn.selected = !_selectBtn.selected;
    if (self.selectBlock) {
        self.selectBlock(_selectBtn.selected);
    }
}
- (void)countAdd {

    int count = _countModifyLabel.text.intValue;
    count ++;
    _countDeleteBtn.enabled = !(count == 1);

    if (self.modifyBlock) {
        self.modifyBlock(count);
    }
    
}
- (void)countDelete {
    
    int count = _countModifyLabel.text.intValue;
    if (count == 1) {
        [ByCoreToolAlert showOnlyText:@"已经不能再少了!"];
        return;
    }
    
    count --;
    // 如果数量为1，-按钮设为无法点击
    _countDeleteBtn.enabled = !(count == 1);
  
    if (self.modifyBlock) {
        self.modifyBlock(count);
    }
}


#pragma mark - 设置属性
- (void)setModel:(ByModelShopCarProductInStore *)model {

    _model = model;

    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _selectBtn.selected = _model.selected;
    [_selectBtn setImageEdgeInsets:UIEdgeInsetsMake((self.height - 20) / 2, 5, (self.height - 20) / 2, 5)];
    
    // 标题
    _titleLabel.text = _model.title;
    // 现价
    _currentPriceLabel.text = [NSString stringWithFormat:@"￥%.2lf", _model.price];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_currentPriceLabel.text];
    [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:[_currentPriceLabel.text rangeOfString:@"￥"]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(_currentPriceLabel.text.length - 2, 2)];
    _currentPriceLabel.attributedText = attrStr;
    // 原价
    _originPriceLabel.text = [NSString stringWithFormat:@"￥%.2lf", _model.originPrice];
    attrStr = [[NSMutableAttributedString alloc] initWithString:_originPriceLabel.text];
    [attrStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle) range:[_originPriceLabel.text rangeOfString:_originPriceLabel.text]];
    _originPriceLabel.attributedText = attrStr;
    // 数量
    _countModifyLabel.text = [NSString stringWithFormat:@"%li", _model.count];
    _countDeleteBtn.enabled = !(_model.count == 1);
    // 配送方式
    _transferLabel.text = [NSString stringWithFormat:@"配送方式：%@", _model.transfer];
    // 附加项
    if ([_model.transfer containsString:@"自取"]) {
        _additionLabel.text = @"查看地址";
    } else {
        _additionLabel.text = @"当日达";
    }
}

@end
